'use strict';

/* Controllers
 * 登录 controller
 */

app.controller('LoginController', ['$scope', '$http', '$state', '$cookieStore', 'InfoUserLoginService' ,'InfoUserAuthService' ,'permissions'  ,'$localStorage' ,function($scope, $http, $state, $cookieStore, InfoUserLoginService ,InfoUserAuthService ,permissions ,$localStorage) {
		$scope.user = {};
		$scope.authError = null;


		$scope.login = function(){

			var promise = InfoUserLoginService.login($scope.user);
			promise.then(function(data){
				if (data.code=='1') {
					InfoUserAuthService.getUserAuthByUserName($scope.user.userName).then(function (data) {
						permissions.setPermissions(data.data);
						// $cookieStore.put("user",$scope.user);
						$localStorage.user = $scope.user;
						$scope.$emit("login.user",$scope.user);
						$state.go('app.index');
					});

				} else {
					$scope.authError = data.message;
				};
			},function (error) {
				$scope.authError = error.data.error.error;
			});
		}

}]);